package com.Telien.app.user.service;

import java.util.ArrayList;
import java.util.List;

import com.Teilen.app.db.main.DBWrapper;
import com.Telien.app.user.object.User;

public class UserManagementService {

	//Mem Storage for Users
	ArrayList<User> systemUsers = new ArrayList();
	//Logger
	public UserManagementService(){
		//Adding System Admin User
		populateUsers();
		User admin = new User("System_Admin","AdminPassword",1);
		systemUsers.add(admin);
		System.out.println();
	}
	
	public String addUser(String username,String password,String firstname,
			String lastname, String phonenumber, String emailaddress){
    	User user = new User(username,password, firstname, lastname, emailaddress, phonenumber,2);
    	systemUsers.add(user);
    	DBWrapper dbWrapper = new DBWrapper();
    	dbWrapper.createUser(user);
    	System.out.println("**Added User to System**");
        return user.getUserUUID();
	}
	
	public boolean addUserMock(User user){
    	boolean valid = validateUser(user);
    	if(valid ==false)
    		return false;
    	else
    		systemUsers.add(user);
    	return true;
	}
	
	private boolean validateUser(User user) {
		if(user.getUserName().isEmpty()){
			return false;
		}
		if(user.getPassword().isEmpty()){
			return false;
		}
		if(user.getFirstName().isEmpty()){
			return false;
		}
		if(user.getLastName().isEmpty()){
			return false;
		}
		if(user.getPhoneNumber().isEmpty()){
			return false;
		}
		if(user.getEmailAddress().isEmpty()){
			return false;
		}
		return true;
	}

	public boolean deleteUser(String userId){
		for(User u : systemUsers){
			if(u.getUserUUID().equals(userId)){
				systemUsers.remove(u);
		    	System.out.println("**Deleted User to System**");
				return true;
			}
		}
		return false;
	}
	
	public boolean updateUser(String userId,String username,String password,String firstname,
			String lastname, String phonenumber, String emailaddress){
		for(User u : systemUsers){
			if(u.getUserUUID().equals(userId)){
				systemUsers.remove(u);
		    	User updatedUser = new User(userId,username,password, firstname, 
		    			lastname, emailaddress, phonenumber, 2);
		    	systemUsers.add(updatedUser);
		    	System.out.println("**Updated User to System**");
		    	return true;
			}
		}
		return false;
	}
	
	public String getUser(String userId){
		for(User u : systemUsers){
			if(u.getUserUUID().equals(userId)){
		    	System.out.println("**Fetched User from System**");
		    	return u.toJson();
			}
		}
		return "";
	}
	
	public String getUserName(String userID){
		DBWrapper dbWrapper = new DBWrapper();
		return dbWrapper.selectUser(userID);
	}
	
	public User getUserObj(String userName){
		for(User u : systemUsers){
			if(u.getUserName().equals(userName)){
		    	System.out.println("**Fetched User from System**");
		    	return u;
			}
		}
		return null;
	}
	
	public User getUserObjFromUUID(String userId){
		for(User u : systemUsers){
			if(u.getUserUUID().equals(userId)){
		    	System.out.println("**Fetched User from System**");
		    	return u;
			}
		}
		return null;
	}
	
	public boolean login(String username,String password){
		for(User u : systemUsers){
			if(u.getUserName().equals(username) && u.getPassword().equals(password)){
				return true;
			}
		}
		return false;
	}
	
	public void loadUsersFromDb(){
		DBWrapper dbWrapper = new DBWrapper();
		systemUsers = (ArrayList<User>) dbWrapper.selectAllUsers();
	}
	
	public String getUserNameMem(String userId){
		for(User u : systemUsers){
			if(u.getUserUUID().equals(userId)){
				return u.getUserName();
			}
		}
		return "";
	}
	
	public String getAllUsers(){
		String json="{\"users\":[";
		for(User u : systemUsers){
		    System.out.println("**Fetched User from System**");
		    	json+=u.toJson()+",";
		}
		json =json.substring(0, json.length()-1);
		json=json+"]}";
		return json;
	}
	
	public void populateUsers(){
		DBWrapper dbWrapper = new DBWrapper();
		List<User> users = dbWrapper.selectAllUsers(); 
		for(User user : users){
			systemUsers.add(user);
		}
	}	
	
	public void printUserIds() {
		for (User u : systemUsers) {
			System.out.println(u.getUserUUID() + " " + u.getUserName());			
		}
	}
}
